package com.itboye.mengwu.app.ui.mymeng.fragment;

import java.text.SimpleDateFormat;
import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.aiwujia.ActivityZengWuXianShouZengFang;

import com.itboye.mengwu.data.model.WodeZengSongShangPingBean;
import com.itboye.mengwu.data.presenter.FenSiPresenter;
import com.itboye.mengwu.widget.BySqlUtil;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

/*****
 * 
 * @author
 * 
 */
@SuppressLint("InflateParams")
public class MyMengMeShouDaoFragment extends BaseFragment implements Observer,
		IXListViewListener {

	XListView myzengosng_ListView;
	FenSiPresenter presenter;
	WodeZengSongShangPingBean bean,beanTwo;
	String to_uid;
	MemengZengSongAdapter adapter;
	private int pageNo = 1;
	boolean ShangLaflag = false;
	MemengZengSongAdapter memengZengSongAdapter;
	String uid;

	@Override
	public int initView() {

		return R.layout.fragment_mymengme_zengsong;
	}

	@Override
	public void onMyActivityCreated() {

		// 刷新
		myzengosng_ListView.setXListViewListener(this);
		myzengosng_ListView.setPullLoadEnable(true);
		myzengosng_ListView.setPullRefreshEnable(true);
		// 提示
		showProgressDialog("数据加载中，请稍后", true);
		presenter = new FenSiPresenter(this);
		bean = new WodeZengSongShangPingBean();
//		to_uid = bean.getTo_uid();
	 uid = SPUtils.get(getContext(), null, SpPublicParams.UID, "") + "";
		bean.setTo_uid(to_uid);
		presenter.onMeShouDao(uid,pageNo+"","10");
		adapter = new MemengZengSongAdapter(getActivity(), bean);
		
		myzengosng_ListView.setOnItemClickListener(new  OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(getContext(), ActivityZengWuXianShouZengFang.class);
				String pid=bean.getList().get(position-1).getId();
				intent.putExtra("pid", pid);
				getActivity().startActivity(intent);
			}
		});

	}

	
	
	@Override
	public void update(Observable observable, Object data) {

		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == FenSiPresenter.ShopSuccess) {
	
				beanTwo = (WodeZengSongShangPingBean) entity.getData();
//				myzengosng_ListView.setAdapter(new MemengZengSongAdapter(
//						getActivity(), bean));
				if (ShangLaflag == true) {
					bean.getList().addAll(beanTwo.getList());
					memengZengSongAdapter.upDate(getActivity(), bean);
					memengZengSongAdapter.notifyDataSetChanged();
				}else {
					memengZengSongAdapter= new MemengZengSongAdapter(getActivity(), beanTwo);
					myzengosng_ListView.setAdapter(memengZengSongAdapter);
					bean = beanTwo;
				}
			}
			try {
				closeProgressDialog();
			} catch (Exception e) {
				ByAlert.alert("数据加载完毕！");
			}
			stopLoad();
		}
	}

	@SuppressLint("SimpleDateFormat")
	class MemengZengSongAdapter extends BaseAdapter {

		Context context;
		WodeZengSongShangPingBean list;

		public MemengZengSongAdapter(Context context,
				WodeZengSongShangPingBean list) {
			this.context = context;
			this.list = list;
		}
		
		public void  upDate(Context context,
				WodeZengSongShangPingBean list) {
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {

			return   list.getList().size();
		}

		@Override
		public Object getItem(int position) {

			return position;
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HouderView houderView;
			if (convertView == null) {
				houderView = new HouderView();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_mymeng_mezengsong, null);
				houderView.txtShopTitle = (TextView) convertView
						.findViewById(R.id.txtShopTitle);
				houderView.txtZengTime = (TextView) convertView
						.findViewById(R.id.txtZengTime);

				houderView.txtMathtoZeng = (TextView) convertView
						.findViewById(R.id.txtMathtoZeng);

				houderView.txtmathCountext = (TextView) convertView
						.findViewById(R.id.txtmathCountext);
				houderView.txtCIty = (TextView) convertView
						.findViewById(R.id.txtCIty);
				houderView.txtregion = (TextView) convertView
						.findViewById(R.id.txtregion);
				houderView.linZengSong = (LinearLayout) convertView
						.findViewById(R.id.linZengSong);
				houderView.imgRoundImageview = (ImageView) convertView
						.findViewById(R.id.imgRoundImageview);
				convertView.setTag(houderView);
			} else {
				houderView = (HouderView) convertView.getTag();
			}
//			String uid = SPUtils.get(context, null, SpPublicParams.UID, "") + "";
//			if (uid.equals(list.getList().get(position).getWinUid())) {
//				
//			}
			houderView.txtShopTitle.setText(list.getList().get(position)
					.getNickname());

			houderView.txtZengTime.setText(new SimpleDateFormat("yyyy-MM-dd")
					.format(Long.parseLong(list.getList().get(position)
							.getUpdatetime()) * 1000));

			houderView.txtMathtoZeng.setText("妈妈赠");

			houderView.txtmathCountext.setText(list.getList().get(position)
					.getDetail());

			houderView.txtCIty.setText(list.getList().get(position).getDetail()
					+ list.getList().get(position).getLocCountry());

			/***
			 * 在数据库中根据邮编查询地区
			 */
			BySqlUtil sqlUtil = new BySqlUtil();
			sqlUtil.openDataBase(context);
			sqlUtil.getProviceName(list.getList().get(position).getLocProvince(), list.getList().get(position).getLocCity(), context);

			houderView.txtregion.setText(sqlUtil.getProvinceName()+ sqlUtil.getCityName());
			
			
			if (list.getList().get(position).getSendOut().equals("0")) {
				houderView.linZengSong
						.setBackgroundResource(R.drawable.wodeznegosng2);
			} else if (list.getList().get(position).getSendOut().equals("1")) {
				houderView.linZengSong
						.setBackgroundResource(R.drawable.wodezengsong1);
			}
			if (list.getList().get(position).getWinTime() != null
					&& list.getList().get(position).getEvaluateTime() != null) {
				if (list.getList().get(position).getWinTime().equals("0")) {
					houderView.linZengSong
							.setBackgroundResource(R.drawable.yishoudao);
				}
				if (list.getList().get(position).getEvaluateTime().equals("0")) {
					houderView.linZengSong
							.setBackgroundResource(R.drawable.daipingjia2);
				} else if (list.getList().get(position).getEvaluateTime()
						.equals("")) {
					houderView.linZengSong
							.setBackgroundResource(R.drawable.yishoudao);
				}
				if (list.getList().get(position).getExpressTime().equals("0")) {
					houderView.linZengSong
							.setBackgroundResource(R.drawable.fahuozhong);
				} else if (list.getList().get(position).getExpressTime()
						.equals("")) {
					houderView.linZengSong
							.setBackgroundResource(R.drawable.yishoudao);
				}
			}

			XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
					+ list.getList().get(position).getHead(),
					houderView.imgRoundImageview);
			return convertView;
		}

	}

	class HouderView {
		private TextView txtShopTitle, txtZengTime, txtMathtoZeng,
				txtmathCountext;
		private TextView txtCIty, txtregion;

		private ImageView imgRoundImageview;

		private LinearLayout linZengSong;
	}

	@Override
	public void onRefresh() {

		pageNo = 1;
		ShangLaflag=false;
		presenter.onMeShouDao(uid,pageNo+"","10");
	}

	@Override
	public void onLoadMore() {

		pageNo++;
		ShangLaflag=true;
		presenter.onMeShouDao(uid,pageNo+"","10");
	}

	public void stopLoad() {

		if (pageNo == 0) {
			// 当列表中没有数据时
			myzengosng_ListView.iniFooder(getActivity());
		} else
			// 松开加载更多
			myzengosng_ListView.stopLoadMore();
		// 加载数据时间格式
		myzengosng_ListView.stopRefresh();

	}

}
